package org.springblade.desktop.utils;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.request.OapiV2UserListRequest;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.dingtalk.api.response.OapiV2UserListResponse;
import org.springblade.desktop.vo.MyResult;

import java.util.Map;

public class MianUtils {
	/**
	 * @param url         url路径
	 * @param accessToken token值
	 * @param params       传递的参数
	 * @return  mainPoUtils  部门
	 * userPoUtile    人员
	 */
	public static MyResult mainPoUtils(String url, String accessToken, Map<String, Object> params) {

		try {
			DefaultDingTalkClient client = new DefaultDingTalkClient(url);
			OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
			if (params.get("DeptId")!=null){
				req.setDeptId(Long.valueOf(params.get("DeptId")+""));
			}
			if (params.get("Language")!=null){
				req.setLanguage(params.get("Language")+"");
			}
			OapiV2DepartmentListsubResponse rsp = client.execute(req, accessToken);
			String s=rsp.getBody();
			MyResult myResult = JSON.parseObject(s, MyResult.class);
			return myResult;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static MyResult userPoUtile(String url, String accessToken, Map<String, Object> params){
		try {
			DefaultDingTalkClient client = new DefaultDingTalkClient(url);
			OapiV2UserListRequest req = new OapiV2UserListRequest();
			if (params.get("DeptId")!=null){
				req.setDeptId(Long.valueOf(params.get("DeptId")+""));
			}
			if (params.get("Cursor")!=null){
				req.setCursor(Long.parseLong(params.get("Cursor")+""));
			}if (params.get("Size")!=null){
				req.setSize(Long.parseLong(params.get("Size")+""));
			}
			OapiV2UserListResponse rsp = client.execute(req, accessToken);
			String s = rsp.getBody();
			MyResult myResult = JSON.parseObject(s, MyResult.class);
			return myResult;
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}

	}

}
